﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace YArchitech.HVAC
{
	public class HYCableTrayConnect
	{
		public bool NewElbowFitting(IList<CableTray> cableTrays)
		{
			bool flag = false;
			bool result;
			try
			{
				if (cableTrays.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = cableTrays[0].Document;
				HYCableTray hycableTray = new HYCableTray(cableTrays[0]);
				HYCableTray hycableTray2 = new HYCableTray(cableTrays[1]);
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
					if (appropriateConnector != null && appropriateConnector2 != null && !appropriateConnector.IsConnected && !appropriateConnector2.IsConnected)
					{
						document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
						flag = true;
					}
				}
				document.Regenerate();
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}
	}
}
